import 'package:flutter/material.dart';
import 'package:im_module/manager/ImChatManager.dart';
import 'package:im_module/provider/im_provider.dart';
import 'package:im_module/widget/AppBarBackground.dart';
import 'package:im_module/widget/ImHeadImageWidget.dart';
import 'package:provider/provider.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_friend_application.dart';

class ApplyFriendListWidget extends StatefulWidget {
  @override
  _ApplyFriendListWidgetState createState() => _ApplyFriendListWidgetState();
}

class _ApplyFriendListWidgetState extends State<ApplyFriendListWidget> {
  @override
  void initState() {
    super.initState();
    //设置消息已读
    ImChatManager().setFriendApplicationRead();
  }

  @override
  Widget build(BuildContext context) {
    return AppBarBackground(
      child: Scaffold(
          appBar: AppBar(
            backgroundColor: Colors.transparent,
            title: Text("申请列表"),
            centerTitle: true,
          ),
          body: Container(
            height: double.infinity,
            color: Colors.white,
            child: Consumer<ImProvider>(builder: (context, _provider, child) {
              if (_provider.applyFriends.isEmpty) {
                return Center(child: Text("暂无数据"));
              } else {
                return Column(
                  children: _provider.applyFriends
                      .map((e) => buildApplyWidget(e))
                      .toList(),
                );
              }
            }),
          )),
    );
  }

  Widget buildApplyWidget(V2TimFriendApplication? application) {
    if (application == null) {
      return SizedBox();
    }
    return ListTile(
      leading: ImHeadImageWidget("${application.faceUrl}"),
      title: Text("${application.nickname}"),
      trailing: ElevatedButton(
        onPressed: () {
          ImChatManager().acceptFriend(application).then((value) {
            if (value) {
              Navigator.pop(context);
            }
          });
        },
        child: Text("同意"),
      ),
    );
  }
}
